//
//  GPPOPView.m
//  Toyohu
//
//  Created by GuoPeng on 16/4/29.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "GPPOPView.h"

#import <POP/POP.h>

typedef struct {
    CGFloat progress;
    CGFloat toValue;
    CGFloat currentValue;
} AnimationInfo;

@interface GPPOPView ()
{
    
}

@property (nonatomic, assign) CGFloat toValue;

//- (void)setup;
- (void)scaleToSmall;
- (void)scaleAnimation;
- (void)scaleToDefault;

@end

@implementation GPPOPView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    UITapGestureRecognizer *tapImageGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSelectCellImage:)];
    self.userInteractionEnabled = YES;
//    tapImageGesture.state = 
//    tapImageGesture.state = UIGestureRecognizerStateBegan;
    [self addGestureRecognizer:tapImageGesture];
}

- (void)doSelectCellImage:(UIGestureRecognizer *)sender
{
    //    UIImageView *imageView = (UIImageView *)sender.view;
    
//    AnimationInfo animationInfo = [self animationInfoForLayer:self.layer];
//    BOOL hasAnimations = self.layer.pop_animationKeys.count;
//    if (hasAnimations && animationInfo.progress < 0.98) {
//        [self pauseAllAnimations:NO forLayer:self.layer];
//        return;
//    }
//    
//    [self.layer pop_removeAllAnimations];
//    if (/*animationInfo.toValue >= 1 &&*/ self.toValue >1) {
//        [self scaleDownView:self];
//        return;
//    }
//    [self scaleUpView:self];
//    NSLog(@"sender.state = %ld",sender.state);
    
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
        {
            [self scaleToSmall];
        }
            break;
            
        case UIGestureRecognizerStateEnded:
        {
            [self scaleAnimation];
        }
            
            break;
            
        case UIGestureRecognizerStateCancelled:
        {
            [self scaleToDefault];
        }
            
            break;
            
        default:
            break;
    }
    
}


#pragma mark - Private instance methods
- (void)scaleToSmall
{
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(.9f, 0.9f)];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSmallAnimation"];
}

- (void)scaleAnimation
{
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
//    if (self.isTabBarButton) {
//        scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(30.f, 30.f)];
//    }else{
        scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(3.f, 3.f)];
//    }
    
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scaleAnimation.springBounciness = 20.0f;
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSpringAnimation"];
}

- (void)scaleToDefault
{
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleDefaultAnimation"];
}

- (void)setHighlighted:(BOOL)highlighted
{
    
}


//- (void)pauseAllAnimations:(BOOL)pause forLayer:(CALayer *)layer
//{
//    for (NSString *key in layer.pop_animationKeys) {
//        POPAnimation *animation = [layer pop_animationForKey:key];
//        [animation setPaused:pause];
//    }
//}
//
//- (void)scaleDownView:(UIView *)view
//{
//    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
//    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
//    self.toValue = 1;
//    scaleAnimation.springBounciness = 20.f;
//    [view.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
//}
//
//- (AnimationInfo)animationInfoForLayer:(CALayer *)layer
//{
//    POPSpringAnimation *animation = [layer pop_animationForKey:@"scaleAnimation"];
//    CGPoint toValue = [animation.toValue CGPointValue];
//    CGPoint currentValue = [[animation valueForKey:@"currentValue"] CGPointValue];
//    
//    CGFloat min = MIN(toValue.x, currentValue.x);
//    CGFloat max = MAX(toValue.x, currentValue.x);
//    
//    AnimationInfo info;
//    info.toValue = toValue.x;
//    info.currentValue = currentValue.x;
//    info.progress = min / max;
//    return info;
//}
//
//-(void)scaleUpView:(UIView *)view
//{
//    POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];
//    positionAnimation.toValue = [NSValue valueWithCGPoint:self.center];
//    [view.layer pop_addAnimation:positionAnimation forKey:@"layerPositionAnimation"];
//    
//    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
//    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.4, 1.4)];
//    self.toValue = 1.4;
//    scaleAnimation.springBounciness = 20.f;
//    [view.layer pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
//}





@end
